package com.ita.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.entity.User;
import com.ita.service.IUserService;
import com.ita.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UpdateServlet
 */
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IUserService service=new UserServiceImpl();         
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		updateUser(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		updateUser(request,response);
	}

	public void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		String action=request.getParameter("action");
		
		if("1".equals(action)){
			String userId=request.getParameter("userId");
			String userName=request.getParameter("userName");
			String password=request.getParameter("password");
			String address=request.getParameter("address");
			String userPic=request.getParameter("userPic");
			System.out.println("hello3");
			
			System.out.println("2 userName:"+userName+":userId"+userId);
			boolean flag=service.updateUser(new User(userId,userName,password,address,userPic));
			System.out.println("3:");
			PrintWriter writer=response.getWriter();
			HttpSession session=request.getSession();
	    	List<User> list=new ArrayList<User>();
			if(flag){
				
				if(session.getAttribute("loginToken")!=null){
		    		List<User> list1=service.showAllUsers();
		    		for(User user1:list1){
		    			list.add(user1);
		    		}
		    	}
				request.setAttribute("list", list);
				request.getRequestDispatcher("alluser.jsp").forward(request, response);
			}
			else{
				writer.write("<h1>update failed</h1>");
			}
		}
//		
		
		else{
			String id=request.getParameter("id");
			User user=service.queryById(id);
			
			request.setAttribute("userId", user.getUserId());
			request.setAttribute("userName", user.getUserName());
			request.setAttribute("password", user.getPassword());
			request.setAttribute("address", user.getAddress());
			request.setAttribute("userPic", user.getUserPic());
			System.out.println("hello");
			request.getRequestDispatcher("updateUser.jsp").forward(request, response);
			System.out.println("hello2");
		}
		
	}
}
